package com.TeamJigsaw.DanteTrail.Views;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.TeamJigsaw.DanteTrail.Models.GameSprite;
import com.TeamJigsaw.DanteTrail.Models.Party;
import com.TeamJigsaw.DanteTrail.Presenters.GameViewController;

public class RiverGameDisplay extends JPanel{
	
	public final int WIDTH = 500;
	public final int HEIGHT = 300;
	private Party party;
	private MainViewFrame parent;
	private GameSprite player;
	private List<GameSprite> obstacles;
	private List<GameSprite> riverAni;
	private Image bg;
	private Random generator;
	private boolean UP;
	private boolean DOWN;
	private boolean LEFT;
	private boolean RIGHT;
	private int riverDistLeft;
	private boolean over = false;
	private boolean lose = false;
	
	public RiverGameDisplay(Party party, MainViewFrame parent) {
		this.parent = parent;
		this.party = party;
		//load images
		Image raft = new ImageIcon("resources\\raft.gif").getImage();
		bg = new ImageIcon("resources\\riverbg.png").getImage();
		generator = new Random();
		
		obstacles = new ArrayList<GameSprite>();
		riverAni = new ArrayList<GameSprite>();
		player = new GameSprite(WIDTH/2, HEIGHT-64, raft);
		
		riverDistLeft = 5000;
		
		setPreferredSize(new Dimension(500,300));
		addKeyListener(new myKeyListener());
		setFocusable(true);
		requestFocus();		
	}
	
	private void update() {
		
		//randomly generate obstacles
		if (generator.nextInt(100) > 80 && obstacles.size() < 10) {
			Image newObstaclePic = new ImageIcon("resources\\rock" + generator.nextInt(3) + ".png").getImage();
			GameSprite newObstacle = new GameSprite(generator.nextInt(WIDTH), 0, newObstaclePic);
			newObstacle.ySpeed = generator.nextInt(5) + 5;
			obstacles.add(newObstacle);
		}
		
		//randomly generate river streaks
		if (generator.nextInt(100) > 50 && riverAni.size() < 100) {
			Image newStreakPic = new ImageIcon("resources\\river" + generator.nextInt(3) + ".png").getImage();
			GameSprite newStreak = new GameSprite(generator.nextInt(WIDTH), 0, newStreakPic);
			newStreak.ySpeed = generator.nextInt(5) + 5;
			riverAni.add(newStreak);
		}
		
		//update the streak locations
		for (int i = 0; i < riverAni.size(); i++) {
			riverAni.get(i).update();
		}
		
		//update the raft location
		player.ySpeed = 0;
		player.xSpeed = 0;
		
		if (UP) {
			player.ySpeed = -3;
			player.update();
		}
		if (DOWN) {
			player.ySpeed = 3;
			player.update();
		}
		if (LEFT) {
			player.xSpeed = -3;
			player.update();
		}
		if (RIGHT) {
			player.xSpeed = 3;
			player.update();
		}
		
		riverDistLeft--;
		
		//update obstacle location and check for collisions
		for (int i = 0; i < obstacles.size(); i++) {
			obstacles.get(i).update();
			
			//hit condition
			if (obstacles.get(i).position.y+obstacles.get(i).height-10 > player.position.y &&
					obstacles.get(i).position.x > player.position.x  &&
					obstacles.get(i).position.x + obstacles.get(i).width < player.position.x + player.width) 
			{
				player.xSpeed = 0;
				player.ySpeed = 0;
				
				lose = true;
				over = true;
				
				obstacles.clear();
				
				break;
			}
			
			if (obstacles.get(i).position.y > HEIGHT) {
				obstacles.get(i).ySpeed = generator.nextInt(5)+3;
				obstacles.get(i).position.y = 0;
			}
		}
		
		//update river streak location
		if(over == false)
		{
			for (int i = 0; i < riverAni.size(); i++) {
				riverAni.get(i).update();
				
				if (riverAni.get(i).position.y > HEIGHT) {
					riverAni.get(i).ySpeed = generator.nextInt(5)+3;
					riverAni.get(i).position.y = 0;
				}
			}
			
			if (riverDistLeft <= 0)
				over = true;
		}
		
	}
	
	public void paint(Graphics g) {
		super.paint(g);

		g.drawImage(bg,0,0,null);
		
		update();
		
		for(GameSprite curSprite: riverAni)
			curSprite.draw(g);
		
		for (GameSprite curSprite: obstacles)
			curSprite.draw(g);
		
		player.draw(g);
		
		g.setColor(Color.black);
		g.fillRect(0,0,WIDTH, 32);
		g.fillRect(WIDTH, 0, 1000, HEIGHT);
		g.fillRect(0, HEIGHT, WIDTH, 1000);
		g.setColor(Color.white);
		g.drawString("Distance Left: " + riverDistLeft, 16, 16);
		
		try {
			Thread.sleep(25);
		}
		catch(Exception e){
			System.out.println("Process Interrupted");
		}
		
		requestFocus();
		if (over == true) {	
			if (lose == true) {
				if(party.getFood() >= 10) {
					party.addFood(-10);
					System.out.println("issue");
				}
				else if (party.getAmmo() >= 10) {
					party.addAmmo(-10);
				}
				else if (party.getAxels() >= 1) {
					party.addAmmo(-1);
				}
				else if (party.getWheels() >= 2) {
					party.addAmmo(-2);
				}
			}
			//win condition
			GameViewController gvc = new GameViewController (party, parent);
			((CardLayout) parent.getContentPane().getLayout()).show(parent.getContentPane(), "Game");
			parent.setTitle("Trail");
			over = true;
			setOpaque(false);
			g.dispose();
		}
		else
			repaint();
	}
	
	
	
	private class myKeyListener implements KeyListener {

		@Override
		public void keyPressed(KeyEvent e) {
			//toggle motion flags to allow for smooth movement
			if (e.getKeyCode() == KeyEvent.VK_UP)
				{UP = false;
				requestFocusInWindow();
				requestFocus();
				}
			else if (e.getKeyCode() == KeyEvent.VK_DOWN)
				{
				DOWN = false;
				requestFocusInWindow();
				requestFocus();
				}
			else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
				{
				RIGHT = true;
				requestFocusInWindow();
				requestFocus();
				}
			else if (e.getKeyCode() == KeyEvent.VK_LEFT)
				{
				
				LEFT = true;
				requestFocusInWindow();
				requestFocus();
				}
		}

		@Override
		public void keyReleased(KeyEvent e) {
			//clear motion flags if key released
			if (e.getKeyCode() == KeyEvent.VK_UP)
				{
				UP = false;
				requestFocusInWindow();
				requestFocus();
				}
			else if (e.getKeyCode() == KeyEvent.VK_DOWN)
				{
				DOWN = false;
				requestFocusInWindow();
				requestFocus();
				}
			else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
				{
				RIGHT = false;
				requestFocusInWindow();
				requestFocus();
				}
			else if (e.getKeyCode() == KeyEvent.VK_LEFT)
				{
				LEFT = false;
				requestFocusInWindow();
				requestFocus();
				}
		}

		@Override
		public void keyTyped(KeyEvent arg0) {}
		
		
		
	}
	
	public static void main(String args[]) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Party party = new Party();
		
		RiverGameDisplay test = new RiverGameDisplay(party, null);
		
		frame.add(test);
		
		frame.setVisible(true);
		frame.pack();
	}
}
